# Maintainer: Yusuke Sasaki <y_sasaki@nuem.nagoya-u.ac.jp>

_realname=googletest
pkgbase="mingw-w64-${_realname}"
pkgname=("${MINGW_PACKAGE_PREFIX}-gtest")
pkgver=1.13.0
pkgrel=1
pkgdesc="Google Testing and Mocking Framework (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
url="https://github.com/google/googletest"
license=('spdx:BSD-3-Clause')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs")
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-python")
source=("${_realname}-${pkgver}.tar.gz"::"https://github.com/google/googletest/archive/v${pkgver}.tar.gz"
        01-fix-pkgconfig-files.patch)
sha256sums=('ad7fdba11ea011c1d925b3289cf4af2c66a352e18d4c7264392fead75e919363'
            '7112f4bebfa4bd6658c5c886242d0dff820d00ff9204edb667ff64618107f84a')

prepare() {
  cd googletest-${pkgver}
  patch -p1 -i ${srcdir}/01-fix-pkgconfig-files.patch
}

build() {
  declare -a extra_config
  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  mkdir -p "${srcdir}/build-${MSYSTEM}-static" && cd "${srcdir}/build-${MSYSTEM}-static"

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX" \
    ${MINGW_PREFIX}/bin/cmake.exe \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      ${extra_config} \
      -DBUILD_SHARED_LIBS=OFF \
      -DPython_EXECUTABLE=${MINGW_PREFIX}/bin/python.exe \
      ../googletest-${pkgver}

  ${MINGW_PREFIX}/bin/cmake.exe --build ./

  mkdir -p "${srcdir}/build-${MSYSTEM}-shared" && cd "${srcdir}/build-${MSYSTEM}-shared"

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX" \
    ${MINGW_PREFIX}/bin/cmake.exe \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      ${extra_config} \
      -DBUILD_SHARED_LIBS=ON \
      -DPython_EXECUTABLE=${MINGW_PREFIX}/bin/python.exe \
      ../googletest-${pkgver}

  ${MINGW_PREFIX}/bin/cmake.exe --build ./
}

package() {
  cd "${srcdir}/build-${MSYSTEM}-static"
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake.exe --install ./

  cd "${srcdir}/build-${MSYSTEM}-shared"
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake.exe --install ./

  mkdir -p "${pkgdir}"/${MINGW_PREFIX}/src/gtest/cmake
  install -m 0644 ${srcdir}/googletest-${pkgver}/googletest/cmake/* "${pkgdir}"${MINGW_PREFIX}/src/gtest/cmake/

  install -Dm 0644 ${srcdir}/googletest-${pkgver}/LICENSE "${pkgdir}"${MINGW_PREFIX}/share/licenses/gtest/LICENSE
  install -Dm 0644 ${srcdir}/googletest-${pkgver}/LICENSE "${pkgdir}"${MINGW_PREFIX}/share/licenses/gmock/LICENSE
}
